// pages/mockexam/theory/post/detail/index.js
const bgMusic = wx.getBackgroundAudioManager();
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    post:'',
    subjectList:[],
    subjectIndex:0,
    questionIndex:0,
    showButton:false,
    flag:true,
    isOpen: false,//播放开关
    starttime: '00:00', //正在播放时长
    duration: '00:00',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      post: options
    })
    wx.setNavigationBarTitle({
      title: options.title,
    })
   
    var _this=this;
    app.paiRequest("/app/extheory/examinate/subjectList", {
      parentCode: options.id
    }).then(function (ress) {
      var subjectList = ress.data;
      _this.setData({
        subjectList: subjectList
      })      
    })
    app.paiRequest("/app/examination/barbecue/user/save", {
      postId: options.id,
      theoryId: options.theoryId
    }).then(function (res) {
      _this.setData({
        theoryUserId: res.data.id
      })
      var type = res.data.type;
      if (type > 1) {
        wx.showToast({
          title: '你已经完成试题答题',
          icon: 'none',
          duration: 3000,
          mask: false
        })
        wx.navigateBack({
          delta: 1
        })
      }

    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("show")
    console.log(this.data.post.scene);
    var _this=this;
    app.paiRequest("/app/examinate/betweenMillis", {
      scene: this.data.post.scene
    }).then(function (ress) {
      console.log(ress);
      var millis=ress.data;
      _this.data.timer = setInterval(function () { 
        millis = _this.timefunction(millis);
        if(millis<=0){
          _this.submitQuestion();
          clearInterval(_this.data.timer);
        }
      }, 1000);
      
    })
  },
  timefunction:function(millis){
    var hour=0;
    var minute=0;
    var seconds=0;
    var returData = millis-1000;
    var se =parseInt(millis/1000);
    if(se>60){
      seconds =parseInt(se%60);
      var m =parseInt(se/60);
      if(m>60){
        minute =parseInt(m%60);
        hour = parseInt(m/60);
      }else{
        minute=m;
      }
    }else{
      seconds=se;
    }
    var nhour = hour >= 10 ? hour : '0' + hour;
    var nminute=minute>=10?minute:'0'+minute;
    var nseconds = seconds >= 10 ? seconds : '0' + seconds;
    var timeNow = nhour + ":" + nminute + ":" + nseconds;
    this.setData({
      timeNow: timeNow
    })
    return returData;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  listenerButtonPlay: function (e) {
    console.log(e);
    var that = this;
    var src=e.currentTarget.dataset.src;
    src = src.replace(',','');
    //bug ios 播放时必须加title 不然会报错导致音乐不播放
    bgMusic.title = '此时此刻'
    bgMusic.epname = '此时此刻'
    bgMusic.src = src;
    bgMusic.onTimeUpdate(() => {
      //bgMusic.duration总时长  bgMusic.currentTime当前进度
      console.log(bgMusic.currentTime)
      var duration = bgMusic.duration;
      var offset = bgMusic.currentTime;
      var currentTime = parseInt(bgMusic.currentTime);
      var min = "0" + parseInt(currentTime / 60);
      var max = parseInt(bgMusic.duration);
      var sec = currentTime % 60;
      if (sec < 10) {
        sec = "0" + sec;
      };
      var maxHou = parseInt(duration/60);
      if (maxHou<10){
        maxHou = "0" + maxHou;
      }
      var maxSec = parseInt(duration % 60);
      if (maxSec<10){
        maxSec = "0" + maxSec;
      }
      var maxtime = maxHou + ":" + maxSec;
      var starttime = min + ':' + sec;   /*  00:00  */
      that.setData({
        offset: currentTime,
        duration: maxtime,
        starttime: starttime,
        max: max,
        changePlay: true
      })
    })
    //播放结束
    bgMusic.onEnded(() => {
      that.setData({
        starttime: '00:00',
        isOpen: false,
        offset: 0
      })
      console.log("音乐播放结束");
    })
    bgMusic.play();
    that.setData({
      isOpen: true,
    })
  },
  //暂停播放
  listenerButtonPause() {
    var that = this
    bgMusic.pause()
    that.setData({
      isOpen: false,
    })
  },
  listenerButtonStop() {
    var that = this
    bgMusic.stop()
  },
  // 进度条拖拽
  sliderChange(e) {
    var that = this
    var offset = parseInt(e.detail.value);
    bgMusic.play();
    bgMusic.seek(offset);
    that.setData({
      isOpen: true,
    })
  },
  // 页面卸载时停止播放
  onUnload() {
    var that = this
    that.listenerButtonStop()//停止播放
    console.log("离开")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  upList:function(){
    var questionIndex = this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList = subjectList[subjectIndex].quetbankList;
    var questionSize = quetbankList.length;
    var subjectSize = subjectList.length;
    if (questionIndex>0){
      this.setData({
        questionIndex: questionIndex - 1
      })
    } else if (questionIndex==0){
      subjectIndex = subjectIndex-1;      
      if (subjectIndex>0){
        var listqt = subjectList[subjectIndex].quetbankList;
        this.setData({
          subjectIndex: subjectIndex,
          questionIndex: listqt.length-1
        })
      } else if (subjectIndex==0){
        var listqu = subjectList[subjectIndex].quetbankList;
        this.setData({
          subjectIndex: subjectIndex,
          questionIndex: listqu.length-1
        })
      }else{
        this.setData({
          subjectIndex: 0,
          questionIndex: 0
        })
      }
    }else{
      this.setData({
        questionIndex: 0
      })
    }
  },
  nextList:function(){
    var questionIndex=this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList=subjectList[subjectIndex].quetbankList;
    var questionSize = quetbankList.length;
    var subjectSize = subjectList.length;
    if (questionIndex >=0) {
      questionIndex=questionIndex + 1;
      if (questionIndex>=questionSize){
        subjectIndex = subjectIndex+1;
        if (subjectIndex >= subjectSize){
          this.setData({
            showButton:true
          })
        }else{
          this.setData({
            subjectIndex: subjectIndex,
            questionIndex:0
          })
        }
      }else{
        this.setData({
          questionIndex: questionIndex
        })
      }
      
    }else{
      this.setData({
        questionIndex: 0
      })
    }
  },
  radioChang:function(e){
    console.log(e);
    var value=e.detail.value;
    var questionIndex = this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList = subjectList[subjectIndex].quetbankList; 
    var question = quetbankList[questionIndex];
    question.answer=value;
    quetbankList[questionIndex] = question;
    subjectList[subjectIndex].quetbankList = quetbankList;
    this.setData({
      subjectList: subjectList
    })
    var options=this.data.post;
    var questionId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    app.paiRequest("/app/examination/barbecue/record/saveRecord", {
      theoryId: options.theoryId,
      questionId: questionId,
      answerId: value,
      questionType: type,
      postId: options.id
    }).then(function (res) {
      console.log(res)
    })
  },
  checkboxChang:function(e){
    console.log(e);
    var value = e.detail.value;
    var questionIndex = this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList = subjectList[subjectIndex].quetbankList;
    var question = quetbankList[questionIndex];
    question.answer1 = 0;
    question.answer2 = 0;
    question.answer3 = 0;
    question.answer4 = 0;
    question.answer5 = 0;
    for (var i = 0; i < value.length;i++){
      var a=value[i];
      question['answer'+a] = a;
    }
    quetbankList[questionIndex] = question;
    subjectList[subjectIndex].quetbankList = quetbankList;
    this.setData({
      subjectList: subjectList
    })

    var options = this.data.post;
    var questionId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    app.paiRequest("/app/examination/barbecue/record/saveRecord", {
      theoryId: options.theoryId,
      questionId: questionId,
      answerId: value,
      questionType: type,
      postId: options.id
    }).then(function (res) {
      console.log(res)
    })
  },
  submitQuestion: function () {
    var _this = this;
    var options = this.data.post;
    var theoryUserId = this.data.theoryUserId;
    app.paiRequest("/app/extheory/examinate/compute", {
      theoryUserId: theoryUserId,
      theoryId: options.theoryId,
      postId:options.id,
      signupId:options.signup
    }).then(function (res) {
      console.log(res)
      var data = res.data;
      _this.setData({
        resultInfo: data,
        flag: false
      })
    })
  },
  conceal: function () {
    this.setData({
      flag: true
    })
    wx.switchTab({
      url: '/pages/barbecue/index',
    })
  },
  deleteRecord:function(e){
    wx.switchTab({
      url: '/pages/barbecue/index',
    })
  }
})